local hex
local unpack_pos
local doo
local version

local string_unpack = string.unpack
local select = select

local function set_pos(...)
    unpack_pos = select(-1, ...)
    return ...
end

local function unpack(str)
    return set_pos(string_unpack(str, hex, unpack_pos))
end

local function unpack_head()
    local flag = unpack 'c4'
    if flag ~= 'W3do' then
        error("不是w3do文件")
    end
    version = unpack 'l'
    if version ~= 8 and version ~= 7 then
        error("错误的ar3mapUnits.doo版本号:"..version)
    end
    local unknow = unpack 'l'
    if unknow ~= 11 then
        error("未知信息错误")
    end
end

local function unpack_obj()
    local id = unpack 'c4'
    local obj = {
        ['ID']  = id,
        ['样式'] = unpack 'l',
        ['位置'] = {unpack 'fff'},
        ['角度'] = math.deg(unpack 'f'),
        ['缩放'] = {unpack 'fff'},
        ['可见'] = unpack 'I1',
        ['玩家'] = unpack 'l',
        ['未知1'] = unpack 'I1',
        ['未知2'] = unpack 'I1',
        ['生命'] = unpack 'l',
        ['魔法'] = unpack 'l',
    }

    if version >= 8 then
        obj['掉落列表'] = unpack 'l'
    end
    -- 设置物品数量
    local count = unpack 'l'
    if count > 0 then
        obj['掉落'] = {}
        for i = 1, count do
            obj['掉落'][i] = {}
            -- 物品设置
            local dropable_items = unpack 'l'
            for n = 1, dropable_items do
                obj['掉落'][i][n] = {
                    ['ID'] = unpack 'c4',
                    ['几率'] = unpack 'l'
                }
            end
        end
    end

    --默认值为12500
    obj['未知3'] = unpack 'l'
    --位置类型 -1 = 正常， -2 = 扎营
    obj['位置类型'] = unpack 'f'
    --英雄等级。非英雄单位和物品设置为1
    obj['等级'] = unpack 'l'

    if version >= 8 then
        obj['力量'] = unpack 'l'
        obj['敏捷'] = unpack 'l'
        obj['智力'] = unpack 'l'
    end

    obj['库存数量'] = unpack 'l'
    if obj['库存数量'] > 0 then
        obj['库存'] = {}
        for i = 1,obj['库存数量'] do
            obj['库存'][i]= {
                    ['位置'] = unpack 'l',
                    ['ID']   = unpack 'c4',
            }
        end
    end

    obj['修改技能数量'] = unpack 'l'
    if obj['修改技能数量'] > 0 then
        obj['修改技能'] = {}
        for i = 1,obj['修改技能数量'] do
            obj['修改技能'][i]= {
                    ['ID']   = unpack 'c4',
                    ['自动使用'] = unpack 'l',
                    ['等级']     = unpack 'l',
            }
        end
    end

	--0 为任何物品 指定 等级跟类型  1 来自随机组 2 是来自自定义列表
    obj['随机类型'] = unpack 'l'
	if obj['随机类型'] == 0 then
		obj['随机级别'] = unpack('I4')
	elseif obj['随机类型'] == 1 then
		obj['随机级别'] = unpack('I8')
    elseif obj['随机类型'] == 2 then
		obj['随机级别'] = unpack('I' .. unpack('l') * 8)
	end
    --local switch = {
    --    [0] = 'l',
    --    [1] = 'll',
    --    [2] = 'lc4l',
    --}
    --obj['随机级别'] = {unpack(switch[obj['随机单位']])}

    obj['自定义颜色'] = unpack 'l'
    --war3map.w3r中目标rect的创建编号。-1 =停用
    obj['编号']      = unpack 'l'

    obj['创建编号'] = unpack 'l'

    doo[#doo + 1] = obj
end

local function unpack_objs()
    local count = unpack 'l'

    for i = 1, count do
        unpack_obj()
    end
end

function test3(params)
    local f = io.open(params, "rb")
    if not f then
        error('未读取到文件')
        return
    end
    hex = f:read 'a'
    unpack_pos = 1

    doo = {}

    unpack_head()
    unpack_objs()

    return doo

end

function test1(params)
    return 'test1:'..params
end
 
function test2(params)
    return 'test2:'..params
end
 
function functionCall(func_name,params)
    local is_true,result
    local sandBox = function(func_name,params)
        local result
        result = _G[func_name](params)
        return result
    end
    is_true,result= pcall(sandBox,func_name,params)
    return result
end
--GB18030
-- functionCall('test3','war3mapUnits.doo')

